package com.jiaoyou.baojian.http;


import com.franmontiel.persistentcookiejar.ClearableCookieJar;
import com.franmontiel.persistentcookiejar.PersistentCookieJar;
import com.franmontiel.persistentcookiejar.cache.SetCookieCache;
import com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor;
import com.jiaoyou.baojian.BaojianApplication;

import java.io.File;
import java.util.concurrent.TimeUnit;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import retrofit2.Callback;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by DemonZYM on 17/5/5.
 */

public class Api {
    private static Api instance;
    private HttpService service;
    public static final String BASE_URL = "http://baofang.xinwen4.com/";
    private OkHttpClient okHttpClient;

    public Api(OkHttpClient okHttpClient) {
        this.okHttpClient = okHttpClient;
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create()) // 添加Gson转换器
                .client(okHttpClient)
                .build();
        service = retrofit.create(HttpService.class);
    }

    public static Api getInstance() {
        if (instance == null)
            instance = new Api(getOkHttpClient());
        return instance;
    }

    private static OkHttpClient getOkHttpClient() {
        //定制OkHttp
        OkHttpClient.Builder httpClientBuilder = new OkHttpClient
                .Builder();
        //设置超时时间
        httpClientBuilder.connectTimeout(15, TimeUnit.SECONDS);
        httpClientBuilder.writeTimeout(15, TimeUnit.SECONDS);
        httpClientBuilder.readTimeout(15, TimeUnit.SECONDS);
        //使用拦截器
        httpClientBuilder.addInterceptor(new RequestInterceptor());
        httpClientBuilder.addInterceptor(new ReceivedCookiesInterceptor());
        //设置cookie持久化
        ClearableCookieJar cookieJar =
                new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(BaojianApplication.getInstance()));
        httpClientBuilder.cookieJar(cookieJar);

        return httpClientBuilder.build();
    }

    public void clearCookie(){
        ((ClearableCookieJar)okHttpClient.cookieJar()).clear();
    }

    public void cancelAllRequest(){
        okHttpClient.dispatcher().cancelAll();
    }

    public void register(String mobile, String password, String vcode, BaofangCallback callback){
        service.register(mobile, password, vcode).enqueue(callback);
    }

    public void login(String mobile, String password, BaofangCallback callback){
        service.login(mobile, password, true).enqueue(callback);
    }

    public void vcodelogin(String mobile, String password, String vcode, BaofangCallback callback){
        service.vcodeLogin(mobile, password, vcode, true).enqueue(callback);
    }

    public void modifyUserInfo(String avata, String[] imageList, String nickname, String sex, String femaleType, String birthday, String description, int costPerMin, BaofangCallback callback){
        service.modifyUserInfo(avata, imageList, nickname, sex, femaleType, birthday, description, costPerMin).enqueue(callback);
    }

    public void getRoomList(int pageNo, int pageSize, BaofangCallback callback){
        service.getRoomList(pageNo, pageSize).enqueue(callback);
    }

    public void getActressList(String type, int pageNo, int pageSize, BaofangCallback callback){
        service.getActressList(type, pageNo, pageSize).enqueue(callback);
    }

    public void getGiftList(BaofangCallback2 callback){
        service.getGiftList().enqueue(callback);
    }

    public void giveGift(long toUserId, long giftId, int quantity, BaofangCallback callback){
        service.giveGift(toUserId, giftId, quantity).enqueue(callback);
    }

    public void giveRoomGift(long roomId, long giftId, int quantity, BaofangCallback callback){
        service.roomGiveGift(roomId, giftId, quantity).enqueue(callback);
    }

    public void giveGifts(long toUserId, String giftList, BaofangCallback callback){
        service.giveGifts(toUserId, giftList).enqueue(callback);
    }

    public void getUserInfo(BaofangCallback callback){
        service.getUserInfo().enqueue(callback);
    }

    public void getUserDetail(long userId, BaofangCallback callback){
        service.getUserDetail(userId).enqueue(callback);
    }

    public void getCurrentUserInfo(BaofangCallback callback){
        service.getCurrentUserInfo().enqueue(callback);
    }

    public void getUserInfoByImUserId(String imUserId, BaofangCallback callback){
        service.getUserInfoByImUserId(imUserId).enqueue(callback);
    }

    public void fav(long userId, BaofangCallback callback){
        service.fav(userId).enqueue(callback);
    }

    public void feedback(String content, BaofangCallback callback){
        service.feedback(content).enqueue(callback);
    }

    public void createRoom(long tribeId, String channelName, String roomCover, String title, String desc, BaofangCallback callback){
        service.roomCreate(channelName, tribeId + "", roomCover, title, desc, "PUBLIC", 0).enqueue(callback);
    }

    public void pickup(String channelName, int fromUserId, BaofangCallback callback){
        service.pickup(channelName, "", "", "点对点聊天", "点对点聊天", "PRIVATE", fromUserId).enqueue(callback);
    }

    public void uploadAvatarImage(File image, BaofangCallback callback){
        RequestBody requestFile =
                RequestBody.create(MediaType.parse("multipart/form-data"), image);
        MultipartBody.Part body =
                MultipartBody.Part.createFormData("imageFile", image.getName() + ".jpg", requestFile);
        service.uploadImage("AVATAR", body).enqueue(callback);
    }
    public void uploadPhotoImage(File image, BaofangCallback callback){
        RequestBody requestFile =
                RequestBody.create(MediaType.parse("multipart/form-data"), image);
        MultipartBody.Part body =
                MultipartBody.Part.createFormData("imageFile", image.getName() + ".jpg", requestFile);
        service.uploadImage("PHOTO", body).enqueue(callback);
    }
    public void uploadRoomBgImage(File image, BaofangCallback callback){
        RequestBody requestFile =
                RequestBody.create(MediaType.parse("multipart/form-data"), image);
        MultipartBody.Part body =
                MultipartBody.Part.createFormData("imageFile", image.getName() + ".jpg", requestFile);
        service.uploadImage("ROOM_BACKGROUND", body).enqueue(callback);
    }
    public void uploadAudio(File audio, BaofangCallback callback){
        RequestBody requestFile =
                RequestBody.create(MediaType.parse("multipart/form-data"), audio);
        MultipartBody.Part body =
                MultipartBody.Part.createFormData("imageFile", audio.getName(), requestFile);
        service.uploadImage("ACTRESS_APPLY_AUDIO", body).enqueue(callback);
    }

    public void applyActress(String audioUrl, String type, int price, BaofangCallback callback){
        service.applyActress(audioUrl, com.jiaoyou.baojian.utils.Utils.exchangeZhuboType(type), price).enqueue(callback);
    }

    public void getGlobalSetting(BaofangCallback callback){
        service.getGlobalSetting().enqueue(callback);
    }

    public void getRankList(String rankType, String rankRange, BaofangCallback callback){
        service.getRankList(rankType, rankRange).enqueue(callback);
    }

    public void actressCharge(BaofangCallback callback){
        service.actressCharge().enqueue(callback);
    }

    public void exitP2PRoom(BaofangCallback callback){
        service.hangup("PRIVATE").enqueue(callback);
    }

    public void exitRoom(BaofangCallback callback){
        service.exitRoom("PUBLIC").enqueue(callback);
    }

    public void exitRoom(long roomId, BaofangCallback callback){
        service.exitRoom(roomId).enqueue(callback);
    }

    public void hangup(BaofangCallback callback){
        service.hangup().enqueue(callback);
    }

    public void checkCard(BaofangCallback callback){
        service.checkCard().enqueue(callback);
    }

    public void getGoods(BaofangCallback2 callback){
        service.getItemListByShopId(1).enqueue(callback);
    }

    public void getMyFavList(BaofangCallback2 callback){
        service.getMyFavList().enqueue(callback);
    }

    public void getFavList(long userId, BaofangCallback2 callback){
        service.getFavList(userId).enqueue(callback);
    }

    public void getMyFansList(BaofangCallback2 callback){
        service.getMyFansList().enqueue(callback);
    }

    public void getFansList(long userId, BaofangCallback2 callback){
        service.getFansList(userId).enqueue(callback);
    }

    public void sendSMS(String mobile, BaofangCallback callback){
        service.sendSMS(mobile).enqueue(callback);
    }

    public void sendLoginSMS(String mobile, BaofangCallback callback){
        service.sendLoginSMS(mobile).enqueue(callback);
    }

    public void tradeSubmit(long skuId, int quantity, BaofangCallback callback){
        service.tradeSubmit(skuId, quantity).enqueue(callback);
    }

    public void tradePay(long[] tids, BaofangCallback callback){
        service.tradePay(tids).enqueue(callback);
    }

    public void appAlipay(String paymentId, BaofangCallback callback){
        service.appAlipay(paymentId).enqueue(callback);
    }

    public void getCardIndex(BaofangCallback callback){
        service.getCardIndex().enqueue(callback);
    }

    public void outerAlipay(String email, BaofangCallback callback){
        service.outerBind("alipay", email, "", "", "", "", "", "", "", "").enqueue(callback);
    }

    public void outerWxpay(String openid, String nickname, String avatar, String gender, String province, String city, BaofangCallback callback){
        service.outerBind("weixin", openid, "", nickname, avatar, "", gender, "", province, city).enqueue(callback);
    }

    public void getMessageList(int pageNo, int pageSize, BaofangCallback callback){
        service.getMessageList("PLATFORM", pageNo, pageSize).enqueue(callback);
    }

    public void roomCall(String callee, BaofangCallback callback){
        service.roomCall(callee).enqueue(callback);
    }

    public void roomCallv1(String callee, BaofangCallback callback){
        service.roomCallv1(callee).enqueue(callback);
    }

    public void getRoomKey(String userId, String channelName, BaofangCallback callback){
        service.getRoomKey(userId, channelName).enqueue(callback);
    }

//    public void getLoginKey(String userId, BaofangCallback callback){
//        service.getLoginKey(userId).enqueue(callback);
//    }

    public void changePassword(String oldp, String newp, BaofangCallback callback){
        service.changePassword(oldp, newp).enqueue(callback);
    }

    public void roomUserEnter(long roomId, BaofangCallback callback){
        service.enterRoom(roomId).enqueue(callback);
    }

    public void roomUserHeartbeat(long roomId, BaofangCallback callback){
        service.roomHeartbeatUser(roomId).enqueue(callback);
    }

    public void roomHeartbeatZhubo(long roomId, BaofangCallback callback){
        service.roomHeartbeatZhubo(roomId).enqueue(callback);
    }

    public void signIn(String token, BaofangCallback callback){
        service.signin("APP", token).enqueue(callback);
    }

    public void withdrawApply(float amount, String type, BaofangCallback callback){
        service.withdrawApply(amount, type).enqueue(callback);
    }
}
